﻿Public Class frmEditCategories
    Dim strsql As String
    Dim _mysql As New ClsSQLhelper
    Public Primary_Code As Integer
    Dim StrName_Before_Edit As String = ""
    Dim StrCode_Before_Edit As String = ""
    Private Sub frmEditCategories_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        StrName_Before_Edit = Me.TxtCategoriesName.Text
        StrCode_Before_Edit = Me.TxtCategoriesCode.Text
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Try
            If Me.TxtCategoriesName.Text = "" Then
                MsgBox("ชื่อต้องไม่เป็นค่าว่าง", MsgBoxStyle.Critical, "กรุณากรอกข้อมูลให้ครบ")
                Exit Sub
            End If

            If Me.TxtCategoriesName.Text = StrName_Before_Edit And Me.TxtCategoriesCode.Text = StrCode_Before_Edit Then
                DialogResult = Windows.Forms.DialogResult.Cancel
                Exit Sub
            End If
            If MsgBox("คุณต้องการบันทึกการเปลี่ยนแปลงใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "กรุณายืนยันการแก้ไข") = MsgBoxResult.No Then Exit Sub
            strsql = "Update categories set Cate_Name = '" & Me.TxtCategoriesName.Text & "',Cate_Code = '" & Me.TxtCategoriesCode.Text & "'  where Cate_ID = " & Me.Primary_Code
            Select Case (_mysql.MySQLExecute(strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "OK_Button_Click"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = strsql
                    frmDebug.ShowDialog()
                    Exit Sub
            End Select
            'Add Log
            strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (12,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "'," & Me.Primary_Code & ") "
            Select Case (_mysql.MySQLExecute(strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "OK_Button_Click"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = strsql
                    frmDebug.ShowDialog()
            End Select
            DialogResult = Windows.Forms.DialogResult.OK
            Me.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
End Class